// pages/result/result.js
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';

import {GetCouponScanApi,GetCouponVifiryApi} from '../../utils/api.js';

import {
  GetOrderDetailApi,orderVerifyApi
} from "../../utils/newApi.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:'',
    businessId:'',
    orderInfo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    that.setData({
      orderId:options.orderId,
      businessId:options.storeId
    })
    this.GetCouponScan();
  },
  GetCouponScan(){
    GetOrderDetailApi({uid:this.data.orderId,orderId:this.data.businessId}).then(res=>{
      let orderTypeCode=0
      let statusCode=0
      switch(res.data.orderType){
        case '积分权益':
          orderTypeCode=1
        break;
        case '积分电商':
          orderTypeCode=2
        break;
        case '团购订单':
          orderTypeCode=3
        break;
        case '会员订单':
          orderTypeCode=4
        break;
        default:
        break;
      }
      switch(res.data.status){
        case '待付款':
          statusCode=1
        break;
        case '已关闭':
          statusCode=2
        break;
        case '待核销':
          statusCode=3
        break;
        case '已完成':
         statusCode=4
        break;
        default:
        break;
      }
       this.setData({
        orderInfo:res.data,
        'orderInfo.orderTypeCode':orderTypeCode,
        'orderInfo.statusCode':statusCode
       })
       console.log(this.data.orderInfo,"orderInfo")
    })
  },
  // 核销
  orderEd(){
    let that = this
    let data={
      uid:this.data.orderId,
      qty:1,
      storeId:this.data.businessId
    }
    orderVerifyApi(data).then(res=>{
      if(res.code==0){
        wx.showToast({
          title: '核销成功',
          icon: 'none',
          duration: 2000
        });
        that.GetCouponScan();
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1500);
      }else{
        wx.showToast({
          icon: "none",
          title: res.message,
          duration: 2000
        })
      }
    })
    // if (that.data.orderInfo.status === 2) {
    //   wx.showToast({
    //     title: '代金券异常，请检查代金券状态',
    //     icon: 'none',
    //     duration: 2000
    //   });
    // } else {
      // this.orderRequest()
    // }
  },

  orderRequest() {
    let that = this;
    GetCouponVifiryApi({id:this.data.orderInfo.id,businessId:this.data.businessId}).then(res=>{
     if(res.code==200){
           wx.showToast({
              title: '核销成功',
              icon: 'none',
              duration: 2000
            });
            that.GetCouponScan();
            setTimeout(function(){
              wx.navigateBack({
                delta: 1,
              })
            },1500);
     }else{
      wx.showToast({
            icon: "none",
            title: res.message,
            duration: 2000
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1500);
     }
    })
    // http.post('/api/store/v1/appointment/update/status', {
    //   id: that.data.orderId,
    //   status: 1 //使用成功
    // }).then(res => {
    //   console.log(res,"修改订单状态")
    //   if(res.code == 200 ){
    //     wx.showToast({
    //       title: '到店成功',
    //       icon: 'none',
    //       duration: 2000
    //     });
    //     setTimeout(function(){
    //       wx.navigateBack({
    //         delta: 1,
    //       })
    //     },1500);
    //   } else {
    //     wx.showToast({
    //       icon: "none",
    //       title: res.message,
    //       duration: 2000
    //     })
    //     setTimeout(function(){
    //       wx.navigateBack({
    //         delta: 1,
    //       })
    //     },1500);
    //   }
    // })
  }
})